package com.lab.labspring.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lab.labspring.domain.LabAdminEntity;
import com.lab.labspring.exception.BusinessException;
import com.lab.labspring.exception.enums.BusinessExceptionEnum;
import com.lab.labspring.req.AdminLoginReq;
import com.lab.labspring.resp.AdminLoginResp;
import com.lab.labspring.service.LabAdminService;
import com.lab.labspring.mapper.LabAdminMapper;
import com.lab.labspring.utils.OldJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Admin
* @description 针对表【lab_admin】的数据库操作Service实现
* @createDate 2024-11-21 16:04:44
*/
@Service
public class LabAdminServiceImpl extends ServiceImpl<LabAdminMapper, LabAdminEntity>
    implements LabAdminService{

    @Autowired
    private LabAdminService labAdminService;
    @Override
    public AdminLoginResp login(AdminLoginReq adminLoginReq) {
        QueryWrapper<LabAdminEntity> labAdminEntityQueryWrapper = new QueryWrapper<>();
        labAdminEntityQueryWrapper.eq("username",adminLoginReq.getUsername())
                .eq("password",adminLoginReq.getPassword());
        LabAdminEntity entity = labAdminService.getOne(labAdminEntityQueryWrapper);
        if(ObjectUtil.isEmpty(entity)){
            throw new BusinessException(BusinessExceptionEnum.ADMIN_NOT_EXIST);
        }
        AdminLoginResp memberLoginResp = BeanUtil.copyProperties(entity, AdminLoginResp.class);
        String token = OldJwtUtil.createToken(adminLoginReq.getUsername(),adminLoginReq.getPassword());
        memberLoginResp.setToken(token);
        return memberLoginResp;
    }
}




